import requests  
from functools import wraps
import time

class GitLabManager:  
    def __init__(self, access_token, gitlab_url):  
        self.access_token = access_token  
        self.gitlab_url = gitlab_url  
        self.headers = {  
            'PRIVATE-TOKEN': self.access_token  
        }

    def response_management(func):  
        @wraps(func)  
        def wrapper(self, *args, **kwargs):  
            start_time = time.time()  # 记录开始时间  
            response = func(self, *args, **kwargs)  
            end_time = time.time()  # 记录结束时间  
            execution_time = end_time - start_time  # 计算执行时间  
            
            if response.status_code in (200, 201):  
                print(f'访问成功: {func.__name__} 方法！执行时间: {execution_time:.2f} 秒')  
                return response.json()  
            else:  
                print(f'访问失败: {func.__name__} 方法！执行时间: {execution_time:.2f} 秒')  
                print('状态码:', response.status_code)  
                print('响应内容:', response.json())  
                return None   
        return wrapper 

    @response_management  
    def get_groups(self):
        """
        列出群组
        """
        url = f'{self.gitlab_url}/api/v4/groups'
        response = requests.get(url, headers=self.headers)

        return response
    
    @response_management  
    def create_groups(self, name):
        """
         创建群组
        """
        url = f'{self.gitlab_url}/api/v4/groups'

        groups_data = {  
            'name': name,  
            'path': name,  
        }  
        response = requests.post(url, headers=self.headers, json=groups_data)

        return response

    @response_management
    def create_project(self, project_name, namespace_id, visibility='private'):  
        """
        创建一个空项目
        project_name: 项目名称
        namespace_id: 空间ID
        visibility: 可见性
        """  
        url = f'{self.gitlab_url}/api/v4/projects'  
        
        project_data = {  
            'name': project_name,  
            'namespace_id': namespace_id,  
            'visibility': visibility  
        }  
        
        response = requests.post(url, headers=self.headers, json=project_data)  
        return response
    
    @response_management
    def project_conf_push_mirror(self, project_id, url):
        """
        配置镜像推送
        import_url: https://username:token@gitlab.company.com/group/project.git
        """  
        url = f'{self.gitlab_url}/api/v4/projects/{project_id}/remote_mirrors'  

        conf_data = {  
            'url': url,  
            'enabled': True
        }  

        response = requests.post(url, headers=self.headers, json=conf_data)    
        
        return response
    
    @response_management
    def project_conf_pull_mirror(self, project_id, import_url):
        """
        配置镜像拉取
        import_url: https://username:token@gitlab.company.com/group/project.git
        """  
        url = f'{self.gitlab_url}/api/v4/projects/{project_id}'  

        conf_data = {  
            'import_url': import_url,  
            'mirror': True
        }  

        response = requests.put(url, headers=self.headers, json=conf_data)    
        return response


    
